
<template>
    <el-form 
    
    ref="form" 
   
    :model="form" 
    
    label-width="80px" 
    enctype="multipart/form-data" >
        

        <div class='picUp'>
            <el-form-item label="顶部">
                <el-upload
                ref="uploadtop"
                action="https://jsonplaceholder.typicode.com/posts/"
                list-type="picture-card"
                :on-preview="handlePictureCardPreview"
                :on-remove="handleRemove"
                :before-upload="beforeuploadtop"
                :multiple="true"
                :auto-upload="false"
                enctype="multipart/form-data"
                :file-list="fileList">
                <i class="el-icon-plus"></i>
                </el-upload>
                <el-dialog :visible.sync="dialogVisible" >
                <img width="100%" :src="dialogImageUrl" alt="">
                </el-dialog>
            </el-form-item>
            
        </div>

        <el-form-item 
        label="跳转路径"
       
         >
            <el-input  v-model="form.url" ></el-input>
        </el-form-item>
        
        <div class='picUp'>
            <el-form-item label="报名框">
                <el-upload
                ref="uploadbg"
                action="https://jsonplaceholder.typicode.com/posts/"
                list-type="picture-card"
                :on-preview="handlePictureCardPreview"
                :on-remove="handleRemove"
                :before-upload="beforeuploadbg"
                :multiple="true"
                :auto-upload="false"
                enctype="multipart/form-data"
                :file-list="fileList">
                <i class="el-icon-plus"></i>
                </el-upload>
                <el-dialog :visible.sync="dialogVisible" >
                <img width="100%" :src="dialogImageUrl" alt="">
                </el-dialog>
            </el-form-item>
            
        </div>

            <div class='picUp'>
                <el-form-item label="底部图集">
                    <el-upload
                    ref="upload"
                    action="https://jsonplaceholder.typicode.com/posts/"
                    list-type="picture-card"
                    :on-preview="handlePictureCardPreview"
                    :on-remove="handleRemove"
                    :before-upload="beforeupload"
                    :multiple="true"
                    :auto-upload="false"
                    enctype="multipart/form-data"
                    :file-list="fileList"
                    :limit="20">
                    <i class="el-icon-plus"></i>
                    </el-upload>
                    <el-dialog :visible.sync="dialogVisible" >
                    <img width="100%" :src="dialogImageUrl" alt="">
                    </el-dialog>
                </el-form-item>
                
            </div>
            
        

        
        

        
        
        
        
        <!-- <Editor @getEditorData="getEditorData"></Editor> -->


        <el-form-item>
            <el-button type="primary" @click="onSubmit">立即创建</el-button>
            <el-button>取消</el-button>
        </el-form-item>
    </el-form>
</template>


<script>
import axios from 'axios'
import { mapState } from 'vuex'
import Editor from 'index/components/Editor'


export default {
    name:'ProcutForm', 
    inject:['reload'], 
    components:{
        Editor 
    },
    data() {
        return {
            fileList:[],
            dialogImageUrl: '',
            dialogVisible: false,
            editorData: '',
            form: {
                url: '',
                
                
            },
            formData: new FormData(),
           
        }
    },
    mounted() {
        
    },
    computed:{
        ...mapState(['baseUrl'])
    },
    methods: {
        
      resetForm(formName) {
        this.$refs[formName].resetFields();
      },
      
        //阻止upload的自己上传，进行再操作
        beforeupload(file) {
            // console.log(file);
            //创建临时的路径来展示图片
            var windowURL = window.URL || window.webkitURL;
            
            this.dialogImageUrl = windowURL.createObjectURL(file);
            
           
           
            this.formData.append('file[]', file)
           
            
            return false;
        },
        //阻止upload的自己上传，进行再操作
        beforeuploadtop(file) {
            // console.log(file);
            //创建临时的路径来展示图片
            var windowURL = window.URL || window.webkitURL;
            
            this.dialogImageUrl = windowURL.createObjectURL(file);
            
           
           
            this.formData.append('top[]', file)
           
            
            return false;
        },
        //阻止upload的自己上传，进行再操作
        beforeuploadbg(file) {
            // console.log(file);
            //创建临时的路径来展示图片
            var windowURL = window.URL || window.webkitURL;
            
            this.dialogImageUrl = windowURL.createObjectURL(file);
            
           
           
            this.formData.append('bg[]', file)
           
            
            return false;
        },
        //覆盖默认的上传行为
        httprequest() {

        },
        handleRemove(file, fileList) {
        // console.log(file, fileList);
        // console.log(fileList);
        },
        handlePictureCardPreview(file) {
            this.dialogImageUrl = file.url;
            this.dialogVisible = true;
            // console.log(file);
        },
        onSubmit() {
            // console.log(this.fileList)
            // return false;
             let that = this
            
            // console.log(param)
            var url = this.form.url;
            
            if(url==''){
                const h = this.$createElement;

                this.$notify({
                title: '警告',
                type: 'warning',
                offset: 100,
                message: h('i', { style: 'color: teal'}, '种类名所有信息必须填写')
                });
                return false;
            }
            //下面append的东西就会到form表单数据的fields中；
            that.formData.append('url', url);
            
             
            
            // console.log(that.formData)
            let config = {
                headers: {
                    'Content-Type': 'multipart/form-data'
                    
                },
                
                
            }
           
            that.$refs.upload.submit();
            that.$refs.uploadtop.submit();
            that.$refs.uploadbg.submit();

            axios.post(this.baseUrl+"cms/addActivated", this.formData, config).then(function(result) {
                            console.log(result);
                            // alert(result.data);

                           

                            if(result.data.result===true){
                                that.$alert('新增成功', '特别提示', {
                                confirmButtonText: '确定',
                                closeOnClickModal:true,
                                center:true,
                                callback: action => {
                                    that.reload();
                                    that.$message({
                                    message: '恭喜你，这是一条成功消息',
                                    type: 'success',
                                    duration:'3000',
                                    showClose:true,
                                    });
                                }
                            });
                                
                            }

                            
            })
            

        }
       
    }
}
</script>
<style lang="stylus">
.el-message-box__wrapper
    z-index 20003!important
</style>
